package lionel.MyJMRI.MyTrain;

import java.util.Iterator;

import lionel.MyBrowser.MyBrowsableElement;
import lionel.MyBrowser.MyBrowsableList;

public class MyTrains implements MyBrowsableList {
	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(MyTrains.class.getName());
	
	MyTrain mCurRunLoc = null;
	
	
	private boolean existAvailable()
	{
		boolean res = false;
		Iterator<MyTrain> ite = MyTrainsInstance.getInstance().iterator();
		while (ite.hasNext() && !res)
				res = ! ite.next().isDisplayed();
		return res;
	}
	
	public synchronized MyBrowsableElement getNext() {
		synchronized ( MyTrainsInstance.getInstance() )
		{
			MyTrain old = mCurRunLoc;
			if (MyTrainsInstance.getInstance().size() == 0)
			{ mCurRunLoc = null; return null; }
			
			if ((mCurRunLoc == null) || ( ! MyTrainsInstance.getInstance().contains(mCurRunLoc)))
				mCurRunLoc = MyTrainsInstance.getInstance().first();
			else
				if (mCurRunLoc == MyTrainsInstance.getInstance().last() )
					mCurRunLoc = MyTrainsInstance.getInstance().first();
				else // Dirty hack to be sure to get next different from current
					mCurRunLoc = (MyTrainsInstance.getInstance().tailSet( new MyTrain( mCurRunLoc.ID()+"\0") ).first() );
			log.debug("is it the same?");
			if ( (MyTrainsInstance.getInstance().size()>1) && (old!=null) && (old.equals(mCurRunLoc)) )
				return getNext();
			log.debug("is it already used by another control?");
			if ( mCurRunLoc.isDisplayed() )
				if ( existAvailable() )
					return getNext();
				else
					return null;
			log.debug("Returning "+mCurRunLoc.ID());
			return ( (MyBrowsableElement) mCurRunLoc );
		}
	}

	public synchronized MyBrowsableElement getPrev() {
		synchronized ( MyTrainsInstance.getInstance() )
		{
			MyTrain old = mCurRunLoc;
			if (MyTrainsInstance.getInstance().size() == 0)
			{ mCurRunLoc = null; return null; }
			
			if ( (mCurRunLoc == null) || ( ! MyTrainsInstance.getInstance().contains(mCurRunLoc)) )
				mCurRunLoc = MyTrainsInstance.getInstance().last();
			else
				if (mCurRunLoc == MyTrainsInstance.getInstance().first() )
					mCurRunLoc = MyTrainsInstance.getInstance().last();
				else // here, sure to have at least 2  elements
					mCurRunLoc = (MyTrainsInstance.getInstance().headSet( mCurRunLoc ).last() );
			log.debug("is it the same?");
			if ( (MyTrainsInstance.getInstance().size()>1) && (old!=null) && (old.equals(mCurRunLoc)) )
				return getPrev();
			log.debug("is it already used by another control?");
			if ( mCurRunLoc.isDisplayed() )
				if ( existAvailable() )
					return getPrev();
				else
					return null;
			log.debug("Returning "+mCurRunLoc.ID());
			return ( (MyBrowsableElement) mCurRunLoc );
		}
	}

	public synchronized MyBrowsableElement select() {
		if (mCurRunLoc == null)
			return null;
		synchronized ( MyTrainsInstance.getInstance() )
		{
			if ( ! MyTrainsInstance.getInstance().contains(mCurRunLoc))
				return null;
			mCurRunLoc.getConductor().releaseThrottle();
			MyTrainsInstance.getInstance().remove(mCurRunLoc);
			mCurRunLoc = null;
			return getNext();
		}

	}

	public void moveCursorTo(MyBrowsableElement elt) {
		mCurRunLoc = (MyTrain) elt;
	}
}
